//
//  ST_StartHomeViewController.swift
//  BTVideo
//
//  Created by DouLei on 2019/10/9.
//  Copyright © 2019 DouLei. All rights reserved.
//

import UIKit
import SwiftyUserDefaults
import SnapKit
import SwifterSwift
import RxSwift
import RxCocoa

class ST_StartHomeViewController: UIViewController ,ST_RegisterRouteProtocol{

    override func viewDidLoad() {
        super.viewDidLoad()
        
        layout_u_i()
        binding()
        
        start_report()
        
        self.hidden_indeterminate_h_u_d()
        
        self.login_finish()
        
    }
    
    private func start_report(){
        
        /// 注册上流程才上报
        guard let account = ST_RegisterMonitor.shared.regist_response_account(),account.registerStep == .longer else{
            return
        }
        
        back_thread_execute(afterDelay: 1) {
            ST_RegisterMonitor.shared.logger.report(fromController: self)
        }
    }
    
    
    
    
    fileprivate let pull_observer = PublishSubject<Void>()
    
    fileprivate let finish_api_pushed = BehaviorRelay<Bool?>(value: nil)
    
    fileprivate let play_finished = BehaviorRelay<Bool?>(value: nil)
    
    var disposeBag = DisposeBag()
    

    fileprivate lazy var welcome_view:LOTAnimationView = {
        
        let path = Bundle.main.path(forResource: "login_ready_go", ofType: "json")!
        
        let control = LOTAnimationView.init(filePath: path)
        
        control.contentMode = UIView.ContentMode.scaleAspectFill
        
//        control.loopAnimation = true
        
        return control
    }()

    
    fileprivate lazy var content_label:UILabel = {
        
        let control = UILabel.init()
        control.text = "太棒啦！！"
        control.textColor = Color.init(hexString: "#333333")
        control.numberOfLines = 0
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var content_label2:UILabel = {
           
       let control = UILabel.init()
       control.text = "让我们开始伴心吧！"
       control.textColor = Color.init(hexString: "#333333")
       control.numberOfLines = 0
       control.textAlignment = .center
       control.font = UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.semibold)
       return control
   }()
        

}


extension ST_StartHomeViewController{
    
    fileprivate func binding(){
        
        let observer = Observable.combineLatest(finish_api_pushed, play_finished)
            
        observer.subscribe(onNext: {[weak self] pushed , played in
            
            if pushed == true && played == true {
                
                self?.hidden_indeterminate_h_u_d()
                
                self?.login_finish()
                
            }else if let p = played,p == true{
                self?.show_indeterminate_h_u_d()
            }
            
        }).disposed(by: disposeBag)
        
        
        Observable.combineLatest(pull_observer, SCComponets.config_component.boot_config_observer).subscribe(onNext: {[weak self] _ , state in
            
            if state == .finished{
                self?.push_resiter_finished()
            }
            
        }).disposed(by: disposeBag)
        
        
        welcome_view.play {[weak self] (f) in
            
            self?.play_finished.accept(true)
        }
        
        pull_observer.onNext(())
        
    }
    
    fileprivate func push_resiter_finished(){
        
        guard let account = ST_RegisterMonitor.shared.regist_response_account() else{
            return
        }
        
        let typeInt = account.registerStep == .longer ? 1 : 2
        
        JVrequest(PH.registerFinishApi,parameters: ["register_type":typeInt],success:  { (json) in
          
            self.finish_api_pushed.accept(true)
            
        }, failure: { (code, err) in
            
            main_thread_execute(afterDelay: 1.5) {
                self.push_resiter_finished()
            }
            
        })
    }
    
    private func login_finish(){
        
        
        guard let account = ST_RegisterMonitor.shared.regist_response_account() else{
            return
        }
        
        ST_BTUserInfoManager.shared.login_ignore_i_m_message(account: account)
        
    }
    
//
//    fileprivate func loginIM(){
//
//
//        guard let account = ST_RegisterMonitor.shared.regist_response_account() else{
//            return
//        }
//
//        ST_BTUserInfoManager.shared.login(account: account, completion: {[weak self] code in
//
//            if code != 1{
//                self?.toast(text: "登录失败，请重启App试试")
//            }
//        })
//
//    }
    
}




extension ST_StartHomeViewController{
    
    
    fileprivate func layout_u_i(){
        
        view.backgroundColor = Color.white
        
        let topEdge = iPhoneXSeries ?  100 : 53
        
        view.addSubview(welcome_view)
        welcome_view.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(33)
            make.right.equalToSuperview().offset(-33)
            make.top.equalToSuperview().offset(topEdge)
            make.height.equalTo(welcome_view.snp.width)
        }
        
        view.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(welcome_view.snp.bottom)
        }
        
        view.addSubview(content_label2)
        content_label2.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(content_label.snp.bottom).offset(15)
        }
        
    }
    
}
